package cn.ulc.socket.common.handler;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import cn.ulc.common.constant.enums.ResultCode;
import cn.ulc.common.exception.PlatformException;
import cn.ulc.common.util.Result;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;

/**
 * 全局的的异常拦截器
 *
 * @author liuBin
 * @date 2018/09/09
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    private static final Log log = LogFactory.get();
    /**
     * 全局异常.
     *
     * @param e the e
     * @return Result
     */

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.OK)
    @ResponseBody
    public Result exception(Exception e) {
        if(e instanceof PlatformException) {
            PlatformException platformException = (PlatformException)e;
            log.error("自定义异常,====={}",e.getMessage());
            if(platformException.getCode() == null) return Result.fail(platformException.getMessage());
            return Result.fail(platformException.getCode(),platformException.getMessage());
        }else {
            log.error("保存全局异常信息 ex====={}", e.getMessage());
            e.printStackTrace();
            return Result.fail(ResultCode.FAIL_UNKNOWN.getCode(),"");
        }
    }
}
